<template>
  <el-dialog
    :title="!dataForm.orderId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户ID" prop="miaoshaOrderUserid">
      <el-input v-model="dataForm.miaoshaOrderUserid" placeholder="用户ID"></el-input>
    </el-form-item>
    <el-form-item label="商品ID" prop="miaoshaOrderGoodsid">
      <el-input v-model="dataForm.miaoshaOrderGoodsid" placeholder="商品ID"></el-input>
    </el-form-item>
    <el-form-item label="收货地址ID" prop="deliveryAddrId">
      <el-input v-model="dataForm.deliveryAddrId" placeholder="收货地址ID"></el-input>
    </el-form-item>
    <el-form-item label="荣誉过来的商品名称" prop="miaoshaGoodsName">
      <el-input v-model="dataForm.miaoshaGoodsName" placeholder="荣誉过来的商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品数量" prop="goodsCount">
      <el-input v-model="dataForm.goodsCount" placeholder="商品数量"></el-input>
    </el-form-item>
    <el-form-item label="商品单价" prop="goodsPrice">
      <el-input v-model="dataForm.goodsPrice" placeholder="商品单价"></el-input>
    </el-form-item>
    <el-form-item label="订单状态" prop="miaoshaOrderStatus">
      <el-input v-model="dataForm.miaoshaOrderStatus" placeholder="订单状态"></el-input>
    </el-form-item>
    <el-form-item label="订单创建时间" prop="miaoshaCreateTime">
      <el-input v-model="dataForm.miaoshaCreateTime" placeholder="订单创建时间"></el-input>
    </el-form-item>
    <el-form-item label="支付时间" prop="miaoshaPayDate">
      <el-input v-model="dataForm.miaoshaPayDate" placeholder="支付时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          miaoshaOrderUserid: '',
          miaoshaOrderGoodsid: '',
          deliveryAddrId: '',
          miaoshaGoodsName: '',
          goodsCount: '',
          goodsPrice: '',
          miaoshaOrderStatus: '',
          miaoshaCreateTime: '',
          miaoshaPayDate: '',
          orderId: 0,
        },
        dataRule: {
          miaoshaOrderUserid: [
            { required: true, message: '用户ID不能为空', trigger: 'blur' }
          ],
          miaoshaOrderGoodsid: [
            { required: true, message: '商品ID不能为空', trigger: 'blur' }
          ],
          deliveryAddrId: [
            { required: true, message: '收货地址ID不能为空', trigger: 'blur' }
          ],
          miaoshaGoodsName: [
            { required: true, message: '荣誉过来的商品名称不能为空', trigger: 'blur' }
          ],
          goodsCount: [
            { required: true, message: '商品数量不能为空', trigger: 'blur' }
          ],
          goodsPrice: [
            { required: true, message: '商品单价不能为空', trigger: 'blur' }
          ],
          miaoshaOrderStatus: [
            { required: true, message: '订单状态不能为空', trigger: 'blur' }
          ],
          miaoshaCreateTime: [
            { required: true, message: '订单创建时间不能为空', trigger: 'blur' }
          ],
          miaoshaPayDate: [
            { required: true, message: '支付时间不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orderId) {
            this.$http({
              url: this.$http.adornUrl(`/group/miaoshaorder/info/${this.dataForm.orderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.miaoshaOrderUserid = data.miaoshaOrder.miaoshaOrderUserid
                this.dataForm.miaoshaOrderGoodsid = data.miaoshaOrder.miaoshaOrderGoodsid
                this.dataForm.deliveryAddrId = data.miaoshaOrder.deliveryAddrId
                this.dataForm.miaoshaGoodsName = data.miaoshaOrder.miaoshaGoodsName
                this.dataForm.goodsCount = data.miaoshaOrder.goodsCount
                this.dataForm.goodsPrice = data.miaoshaOrder.goodsPrice
                this.dataForm.miaoshaOrderStatus = data.miaoshaOrder.miaoshaOrderStatus
                this.dataForm.miaoshaCreateTime = data.miaoshaOrder.miaoshaCreateTime
                this.dataForm.miaoshaPayDate = data.miaoshaOrder.miaoshaPayDate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/group/miaoshaorder/${!this.dataForm.orderId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'miaoshaOrderUserid': this.dataForm.miaoshaOrderUserid,
                'miaoshaOrderGoodsid': this.dataForm.miaoshaOrderGoodsid,
                'deliveryAddrId': this.dataForm.deliveryAddrId,
                'miaoshaGoodsName': this.dataForm.miaoshaGoodsName,
                'goodsCount': this.dataForm.goodsCount,
                'goodsPrice': this.dataForm.goodsPrice,
                'miaoshaOrderStatus': this.dataForm.miaoshaOrderStatus,
                'miaoshaCreateTime': this.dataForm.miaoshaCreateTime,
                'miaoshaPayDate': this.dataForm.miaoshaPayDate,
                'orderId': this.dataForm.orderId || undefined,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
